package com.baosight.sczx.zm.cb.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cb.Coalseam;
import com.baosight.sczx.common.zm.cInterface.cb.RoadWay;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS;
import com.baosight.sczx.zm.cb.domain.ZMCBRoadway;
import com.baosight.sczx.zm.cb.domain.ZMCBWorkingface;


/**
 * 巷道 修改
 * <p>Title: ServiceZMCB07.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 高强
 * @version 1.0
 * <pre>Histroy:
 *          2013-07-18 上午11:14:15  高强  Create
 *</pre>
 */
public class ServiceZMCB07 extends ServiceEPBase {

	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCB07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-07-18 上午11:14:15  高强
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo)
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayType", false));//查询区'巷道类型'
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineRock", false));//煤岩别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.supportingType", true));//支护形式
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayUsing", false));//巷道用途
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.grubProcess", true));//掘进工艺
		outInfo.set("roadwayNoType", SystemSettingCS.getInstance().getRoadwayNo());//巷道编号形式 0自动 1手动 2生成
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.sectionShape",true));//断面形状
		outInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());//巷道所属下拉框
		outInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));//施工状态
		return outInfo;
	}
	
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP08</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-07-18 上午11:14:15  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdKeyId = inInfo.getString("fdParentId");
		String[] _nodeId=fdKeyId.split("_");
		fdKeyId=_nodeId[0];
		if(fdKeyId != null){
			StaticTools.setQueryParameters(inInfo, "fdKeyId", fdKeyId, true);//添加查询条件
		    }
		EiInfo outInfo = super.query(inInfo, "ZMCBRoadway.query",new ZMCBRoadway());
		outInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());
		return outInfo;
	}
	
	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZP07</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-07-18 上午11:14:15  高强
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
       Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
       // 机构名称、机构编号是否重复
		boolean isRepeat = validate(inInfo, "ZMCBRoadway.validate", params);
		if(isRepeat) {
			return inInfo;
		}
		else {
			EiInfo outInfo = super.update(inInfo, "ZMCBRoadway.update");
			return outInfo;
		}
	}
	
	/**
	 * 校验巷道编号、机构名称是否重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-07-18 上午11:14:15  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdAbbreviation = inInfo.getBlock("result").getCellStr(0, "fdAbbreviation");
		
		params.put("fdNo", fdNo);
		params.put("fdName", fdName);
		params.put("fdAbbreviation", fdAbbreviation);
		
		List result = dao.query("ZMCBRoadway.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCBRoadway _temp = (ZMCBRoadway)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)){
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdAbbreviation() != null && _temp.getFdAbbreviation().equals(fdAbbreviation)) {
				msg.append(" 简称 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
}
